<template>
  <div class="block">
    <!-- <el-tree
      :data="data"
      node-key="id"
      default-expand-all
      :expand-on-click-node="false"
      :render-content="renderContent"
    >
    </el-tree> -->
    <el-tree
      :data="data"
      :default-expand-all="true"
      @node-click="handleNodeClick"
    ></el-tree>
    <!-- <el-button
      size="mini"
      type="text"
      icon="el-icon-edit"
      @click="handleUpdate(scope.row)"
      v-hasPermi="['taa:infclass:edit']"
      >+</el-button
    > -->
  </div>
</template>
<!-- <script src=""></script> -->
<script>
import {
  addChapter,
  delChapter,
  addCatalogue,
  delCatalogue,
} from "@/api/taa/claDetails";
let id = 1000;
export default {
  created() {
    this.asd();
    // console.log('asdasd', this.putChapterInf);
    // this.putChapterInf;
    // this.handleNodeClick()
  },
  // 接受父组件的值
  props: {
    putChapterInf: Array,
    required: true,
  },
  data() {
    return {
      // data: JSON.parse(JSON.stringify(data)),
      data: [],
      getChapterInf: [],
      firstData: {},
      defaultInf: {}
    };
  },
  watch: {
    putChapterInf(newValue, oldValue) {
      this.getChapterInf = newValue;
      // console.log(newValue);
      this.data = [
        {
          id: newValue[0].id,
          label: newValue[0].title,
          children: [
            {
              id: 11,
              label: newValue[0].children[0].catalogueName,
              inf: newValue[0].children[0],
            },
            {
              id: 12,
              label: newValue[0].children[1].catalogueName,
              inf: newValue[0].children[1],
            },
            {
              id: 13,
              label: newValue[0].children[2].catalogueName,
              inf: newValue[0].children[2],
            },
          ],
        },
        {
          id: newValue[1].id,
          label: newValue[1].title,
          children: [
            {
              id: 21,
              label: newValue[1].children[0].catalogueName,
              inf: newValue[1].children[0],
            },
          ],
        },
      ];
      this.defaultInf = newValue[0].children[0]
    this.$emit("change", this.defaultInf);
    },
  },
  methods: {
    asd() {
    },
    handleNodeClick(data) {
      // console.log('asd', data.inf);
      this.firstData = data.inf
      this.$emit("change", this.firstData);
    },
    append(data) {
      const newChild = { id: id++, label: "testtest", children: [] };
      if (!data.children) {
        this.$set(data, "children", []);
      }
      data.children.push(newChild);
    },

    remove(node, data) {
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const index = children.findIndex((d) => d.id === data.id);
      children.splice(index, 1);
    },

    renderContent(h, { node, data, store }) {
      return (
        <span class="custom-tree-node">
          <span>{node.label}</span>
          <span>
            <el-button
              size="mini"
              type="text"
              on-click={() => this.append(data)}
            >
              Append
            </el-button>
            <el-button
              size="mini"
              type="text"
              on-click={() => this.remove(node, data)}
            >
              Delete
            </el-button>
          </span>
        </span>
      );
    },
  },
};
</script>
<style>
.block {
  position: absolute;
  top: 0;
  left: 0;
  width: 22%;
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>